package com.lanren.familybookapp.ui.user.presenter;

import android.text.TextUtils;

import com.lanren.common.base.impl.BasePresenter;
import com.lanren.common.utils.StringUtils;
import com.lanren.familybookapp.ui.user.interfaces.IRegisterModel;
import com.lanren.familybookapp.ui.user.model.RegisterModel;

/**
 * @ClassName RegisterPresenter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/5 9:33
 * @Version 1.0
 */
public class RegisterPresenter extends BasePresenter<IRegisterModel.IRegisterView> {
    private RegisterModel registerModel;

    public RegisterPresenter() {
        registerModel = new RegisterModel();
    }

    public void register(String userName, String password, String verifyPassword) {
        mRootView.showLoading();
        if (TextUtils.isEmpty(userName)) {
            mRootView.showMessage("用户名不能为空");
            return;
        }
        if (StringUtils.containCn(userName)) {
            mRootView.showMessage("用户名不能包含中文");
            return;
        }
        if (userName.length() < 6) {
            mRootView.showMessage("用户名长度不能小于6位");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            mRootView.showMessage("密码不能为空");
            return;
        }
        if (password.length() < 6) {
            mRootView.showMessage("密码长度不能小于6位");
            return;
        }
        if (TextUtils.isEmpty(verifyPassword)) {
            mRootView.showMessage("确认密码不能为空");
            return;
        }
        if (!verifyPassword.equals(password)) {
            mRootView.showMessage("确认密码与密码不一致");
            return;
        }

        registerModel.register(userName, password, new IRegisterModel.RegisterModel() {
            @Override
            public void onError(String data) {
                mRootView.showMessage(data);
            }

            @Override
            public void onSuccess() {
                mRootView.onSuccess();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (registerModel != null) {
            registerModel.clearDisposable();
            registerModel = null;
        }
    }
}
